package com.wpp.controller;

import com.wpp.dao.FavoriteWebsiteDao;
import com.wpp.domain.FavoriteBox;
import com.wpp.domain.FavoriteWebsite;
import com.wpp.domain.User;
import com.wpp.service.FavoriteBoxService;
import com.wpp.service.FavoriteWebsiteService;
import com.wpp.webutil.util.MyUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/favorite")
public class FavoriteController {

    @Resource
    private FavoriteWebsiteService favoriteWebsiteService;

    @Resource
    private FavoriteBoxService favoriteBoxService;

    @GetMapping("/getOne")
    public FavoriteWebsite getOne(Integer id){
        return favoriteWebsiteService.findById(id);
    }

    /**
     * 获得收藏夹展示页面需要的信息
     * @return
     */
    @GetMapping("/getShowInfo")
    public Map<String, Object> getShowInfo(Integer boxId,HttpSession session){
        //返回的map
        HashMap<String, Object> resMap = new HashMap<>(2);
        //获得当前用户的全部收藏夹
        User loginUser = (User) session.getAttribute("loginUser");
        FavoriteBox box = new FavoriteBox();
        box.setDeleted(0);
        box.setUserId(loginUser.getId());
        List<FavoriteBox> boxes = favoriteBoxService.findAll(box);
        resMap.put("boxes",boxes);

        //根据参数上的boxId从数据库查询
        FavoriteBox dbBox = null;
        //如果没有boxId,获得用户书签
        if (boxId==null){
            dbBox = favoriteBoxService.getBookMarkId(loginUser.getId());
        }else{
            //获得boxId里的网站
            dbBox = favoriteBoxService.findOne(boxId);
            //如果没有这个收藏夹 || 该收藏夹不属于当前用户
            //获得他的书签
            if ( dbBox==null || !dbBox.getUserId().equals(loginUser.getId())){
                //获得用户的书签
                dbBox = favoriteBoxService.getBookMarkId( loginUser.getId() );
            }
        }

        //根据dbBoxId查找网址
        List<FavoriteWebsite> websites = favoriteWebsiteService.findByBoxId(dbBox.getId());
        resMap.put("websites",websites);
        //真实查询的boxId
        resMap.put("currentBox",dbBox);
        return resMap;
    }

    /**
     * 获得用户收藏的书签
     * @return
     */
    @RequestMapping("/getBookmark")
    public HashMap<String, Object> getBookmark(HttpServletRequest request){
        HttpSession session = request.getSession();
        System.out.println(session.getId());
        //返回的map
        HashMap<String, Object> resMap = new HashMap<>(2);
        //获得当前用户
        User loginUser = (User) session.getAttribute("loginUser");
        //获得当前用户书签夹
        System.out.println(loginUser);
        FavoriteBox markBox = favoriteBoxService.getBookMarkId(loginUser.getId());


        //用户书签文件夹里面收藏的网页
        List<FavoriteWebsite> favoriteWebsites = favoriteWebsiteService.findByBoxId(markBox.getId());
        resMap.put("favoriteWebsites",favoriteWebsites);
        resMap.put("bookMarkId",markBox.getId());

        return resMap;
    }

    /**
     * 为当前用户添加收藏
     * @return
     */
    @PostMapping("/addFavorite")
    public boolean addFavorite(@RequestBody FavoriteWebsite website) throws IOException {
        this.favoriteHandle(website);

        favoriteWebsiteService.insert(website);
        return true;
    }

    /**
     * 根据id删除一个收藏
     * @param id
     * @return
     */
    @DeleteMapping("/deleteFavorite")
    public boolean deleteFavorite(Integer id){
        return favoriteWebsiteService.deleteById(id);
    }

    /**
     * 更新一个收藏
     * @return
     */
    @PutMapping("/updateFavorite")
    public boolean updateFavorite(@RequestBody FavoriteWebsite website) throws IOException {
        this.favoriteHandle(website);
        favoriteWebsiteService.update(website);
        return true;
    }


    private void favoriteHandle(FavoriteWebsite website) throws IOException {
        URL url = new URL(website.getUrl());
        //图片是否为空
        if ( MyUtil.isEmptyString(website.getImg()) ){
            String icon = url.getProtocol()+"://"+url.getHost()+"/favicon.ico";
            website.setImg(icon);
        }
        //标题是否为空
        if ( MyUtil.isEmptyString(website.getName()) ){
            //爬取标题
            Document parse = Jsoup.parse(url,5000);
            String title = parse.getElementsByTag("title").text();

            website.setName(title);
        }
    }
}
